package com.whcoding.international.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @program: International
 * @description:
 * @author: whcoding
 * @create: 2022-08-02 15:36
 **/

public class MyLocaleResolver implements LocaleResolver {
	//解析请求可以在链接上携带区域信息
	@Override
	public Locale resolveLocale(HttpServletRequest request) {
		String language = request.getParameter("l");
		// 如果没有获取到就使用系统默认的
		Locale locale = Locale.getDefault();
		//如果请求链接不为空
		if (!StringUtils.isEmpty(language)) {
			//分割请求参数
			String[] split = language.split("_");
			//国家，地区
			locale = new Locale(split[0], split[1]);
		}
		return locale;
	}

	@Override
	public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

	}
}
